package com.ajk.server.service.impl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ajk.server.dao.TestConfigMapper;
import com.ajk.server.model.TestConfig;
import com.ajk.server.model.query.TestConfigQuery;
import com.ajk.server.service.TestConfigService;
import com.ajk.server.utils.ServiceAssert;

/**
 * 自动评审结果-配置  <br>
 * t_test_config <br>
 * v1.0 2016-08-26
 */
 @Service
 public class TestConfigServiceImpl implements TestConfigService {
	
	private static final Logger logger = LoggerFactory.getLogger(TestConfigServiceImpl.class);
	
	@Autowired
	private TestConfigMapper testConfigMapper;
	
	/**
 	*根据ID删除 自动评审结果-配置
 	*/
 	@Override
 	public Integer deleteTestConfigById(Integer id){
 		return testConfigMapper.deleteById(id);
 	}
 	
 	/**
 	*新增 自动评审结果-配置
 	*/
 	@Override
 	@Transactional
 	public Integer batchInsertTestConfig(List<? extends TestConfig> testConfigs){
		Integer count = testConfigMapper.batchInsert(testConfigs);
		return count;
 	}
 	/**
 	*新增 自动评审结果-配置
 	*/
 	@Override
 	@Transactional
 	public Integer insertTestConfig(TestConfig[] testAnswer,Integer paperId){
 		
 		testConfigMapper.deleteByCategoryId(paperId);
 		
 		if (ArrayUtils.isNotEmpty(testAnswer)) {
			for (TestConfig domain : testAnswer) {
				domain.setCategoryId(paperId);
				ServiceAssert.throwException(StringUtils.isEmpty(domain.getDoctorSuggest()), "医生建议不能为空");
				ServiceAssert.throwException(domain.getStartRate()==null || domain.getStartRate()<0 || domain.getStartRate()>1.0, "起始比例为空或不合法");
				ServiceAssert.throwException(domain.getEndRate()==null || domain.getEndRate()<0 || domain.getEndRate()>1.0, "终止比例为空或不合法");
			}
		}
		for (TestConfig domain : testAnswer) {
			for (TestConfig domain1 : testAnswer) {
				if(domain!=domain1){
					if((domain.getEndRate()>domain1.getStartRate() && domain.getEndRate()<=domain1.getEndRate()) || (domain1.getEndRate()>domain.getStartRate() && domain1.getEndRate()<domain.getEndRate())){
						ServiceAssert.throwException("比例出现交叉现象，请检查");
					}
				}
			}
		}
 		
		Integer count = testConfigMapper.batchInsert(Arrays.asList(testAnswer));
		return count;
 	}
 	
 	

 	/**
 	*更新 自动评审结果-配置
 	*/
 	@Override
 	@Transactional
 	public Integer updateTestConfig(TestConfig domain){
		Integer count = testConfigMapper.update(domain);
		return count;
 	}
 	
 	/**
 	*根据ID查询 自动评审结果-配置
 	*/
 	public TestConfig findTestConfigById(Integer id){
 		TestConfig result = testConfigMapper.findById(id);
 		if(result!=null){
 			result.dealWith();
 		}
 		return result;
 	}

	/**
 	*通过query 查询 自动评审结果-配置
 	*/
	public List<TestConfig> findTestConfigByQuery(TestConfigQuery query){
		query.dealWith();
		List<TestConfig> result = testConfigMapper.findByQuery(query);
		if(CollectionUtils.isNotEmpty(result)) {
			for (TestConfig testConfig : result) {
				testConfig.dealWith();
			}
		}
		return result;
	}	
	
	/**
 	*通过query count总条数
 	*/
 	public Integer countTestConfigByQuery(TestConfigQuery query){
 		query.dealWith();
 		Integer result = testConfigMapper.countByQuery(query);
 		return result;
 	}
 	
 	/**
 	* 通过ids查询
 	*/
 	public List<TestConfig> findTestConfigByIds(List<Integer> ids){
 		List<TestConfig> result = testConfigMapper.findByIds(ids);
		if(CollectionUtils.isNotEmpty(result)) {
			for (TestConfig testConfig : result) {
				testConfig.dealWith();
			}
		}
		return result;
 	}

	@Override
	public Map<Integer, List<TestConfig>> findTestConfigByCategoryIds(List<Integer> ids) {
		List<TestConfig> result = testConfigMapper.findTestConfigByCategoryIds(ids);
		Map<Integer, List<TestConfig>> map = new HashMap<>();
		if(CollectionUtils.isNotEmpty(result)) {
			for (TestConfig testConfig : result) {
				if(!map.containsKey(testConfig.getCategoryId())){
					map.put(testConfig.getCategoryId(), new ArrayList<>());
				}
				map.get(testConfig.getCategoryId()).add(testConfig);
			}
		}
		
		for(List<TestConfig> configs : map.values()){
			if (CollectionUtils.isNotEmpty(configs)) {
				Collections.sort(configs, new Comparator<TestConfig>() {

					@Override
					public int compare(TestConfig o1, TestConfig o2) {
						return o2.getEndRate().compareTo(o1.getEndRate());
					}
				});
			}
		}
		
		return map;
	}
 	
 	
 	
}	